package com.zhijian.medical.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author HCG
 * @version 1.0
 * @date 2022/2/10
 */
public class StrUtil {

    public static String join(CharSequence delimiter,
                              Iterable<? extends Long> elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        StringJoiner joiner = new StringJoiner(delimiter);
        for (Long cs : elements) {
            joiner.add(cs + "");
        }
        return joiner.toString();
    }

    public static String joinInteger(CharSequence delimiter,
                              Iterable<? extends Integer> elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        StringJoiner joiner = new StringJoiner(delimiter);
        for (Integer cs : elements) {
            joiner.add(cs + "");
        }
        return joiner.toString();
    }

    public static boolean isNullStr(String s) {
        return "null".equalsIgnoreCase(s);
    }

    public static boolean isNotNullStr(String s) {
        return !isNullStr(s);
    }

    /**
     * 从字符串里提取数字
     *
     * @param source
     * @return
     */
    public static Integer drawNumberFromStr(String source) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(source);
        String replaceAll = m.replaceAll("");
        return StringUtils.isBlank(replaceAll) ? -1 : Integer.valueOf(replaceAll);
    }

}
